package main

import (
	"fmt"
	"gitee.com/chenwl888/gotools/utils/log"
	"time"
)

func main() {
	processName := "test_asynchronous_call"
	logger, err := log.NewLogger(processName, 10)
	if err != nil {
		fmt.Println("Failed to create logger:", err)
		return
	}
	defer logger.Close()

	asynchCall(func() error {
		time.Sleep(11 * time.Second)
		return nil
	}, func(result interface{}, err error) {
		if err != nil {
			logger.Error("This is a log message. %v", err)
		}
		logger.Info("------------------------")
	})
	time.Sleep(11 * time.Second)
	select {} // 业务逻辑
}
func asynchCall(asyncCb func() error, cb func(result interface{}, err error)) {
	type callParam struct {
		result interface{}
		err    error
	}
	done := make(chan *callParam)
	go func() {
		e := asyncCb()
		data := &callParam{
			err: e,
		}
		done <- data
	}()

	select {
	case data := <-done:
		if nil != data {
			cb(data.result, data.err)
		}
	case <-time.After(5 * time.Second):
		cb(nil, fmt.Errorf("超时执行"))
	}
}
